package com.example.a02back.controller;

import com.example.a02back.common.APIResponse;
import com.example.a02back.service.impl.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName UpdateController
 * @Description
 * @Author SimpChaos
 * @Date 2022/7/14 17:29
 * @Version 1.0
 **/
@RestController
@ResponseBody
@Controller
@RequestMapping("/update")
public class UpdateController {

    @Resource
    private CardServiceImpl cardService;
    @Resource
    private TradeServiceImpl tradeService;
    @Resource
    private TopicServiceImpl topicService;
    @Resource
    private UserServiceImpl userService;
    @Resource
    private AuthServicelmpl auth;
    @Resource
    private FavoriteItemServiceImpl favoriteItem;
    @Resource
    private FavoriteTopicServiceImpl favoriteTopic;

    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 7:19
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/updatecard")
    public APIResponse<?> updatecard(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String department = httpServletRequest.getParameter("department");
        String position = httpServletRequest.getParameter("position");
        Long cardId = Long.valueOf(httpServletRequest.getParameter("cardId"));
        return APIResponse.success("success",cardService.updateCard(cardId,department,position));
    }

    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 7:19
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/deletecard")
    public APIResponse<?> deletecard(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String cardId = httpServletRequest.getParameter("cardid");
        return APIResponse.success("success",cardService.deleteCard(cardId));
    }


    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/16 2:58
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/choosecard")
    public APIResponse<?> ChooseCard(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String cardid = httpServletRequest.getParameter("cardid");
        return APIResponse.success("success",userService.choose(cardid));
    }

    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/16 2:58
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/favItem")
    public APIResponse<?> favoriteItem(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String sid = httpServletRequest.getParameter("sid");
        return  APIResponse.success("success",favoriteItem.addfavoriteItem(uid,sid));
    }

    /**
     * @Description:
     * @Author: SimpChaos
     * @Date: 2022/7/16 2:58
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/favTopic")
    public APIResponse<?> favoriteTopic(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String topicid = httpServletRequest.getParameter("aid");
        return  APIResponse.success("success",favoriteTopic.addfavoriteTopic(uid,topicid));
    }


/**
 * @Description: 修改商品基本信息
 * @Author: SimpChaos
 * @Date: 2022/7/15 23:15
 * @Param: [httpServletRequest, httpServletResponse]
 * @Return: com.example.a02back.common.APIResponse<?>
 */
    /**
     *这里要注意后期可以加一个cut，检测token是否为本人
     */
    @CrossOrigin
    @RequestMapping("/Trade")
    public APIResponse<?> updateTrade(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String title = httpServletRequest.getParameter("title");
        String sid = httpServletRequest.getParameter("sid");
        String info = httpServletRequest.getParameter("info");
        Integer type = Integer.parseInt(httpServletRequest.getParameter("type"));
        String deadline = httpServletRequest.getParameter("deadline");
        Integer price = Integer.parseInt(httpServletRequest.getParameter("price"));
        String result = tradeService.update(sid, title, info, type,deadline,price);
        return APIResponse.success("success",result);
    }


    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 7:19
     * @Param: [httpServletRequest, httpServletResponse, file]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/TradeImg")
    public APIResponse<?> updateTradeImg(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, MultipartFile file) throws IOException {
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String sid = httpServletRequest.getParameter("sid");
        Integer number = Integer.parseInt(httpServletRequest.getParameter("index"));
        String result = tradeService.updateImg(file,number,uid,sid);
        return APIResponse.success("success",result);
    }


    /**
     * @Description: 撤销发布的东西
     * @Author: SimpChaos
     * @Date: 2022/7/16 1:35
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/deleteT")
    public APIResponse<?> deleteTrade(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String sid = httpServletRequest.getParameter("sid");
        String result = tradeService.delete(sid);
        return APIResponse.success("success",result);
    }



    /**
     * @Description: 修改活动
     * @Author: SimpChaos
     * @Date: 2022/7/15 10:02
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    /**
     *这里要注意后期可以加一个cut，检测token是否为本人
     */
    @CrossOrigin
    @RequestMapping("/topic")
    public APIResponse<?> updateTopic(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){

        String topicId = httpServletRequest.getParameter("aid");
        String headline = httpServletRequest.getParameter("headline");
        String content = httpServletRequest.getParameter("content");
        String time = httpServletRequest.getParameter("time");
        Integer activeValue = Integer.parseInt(httpServletRequest.getParameter("activeValue"));
        String actionAddress = httpServletRequest.getParameter("actionAddress");
        String actionPosition = httpServletRequest.getParameter("actionPosition");

        return  APIResponse.success("success",topicService.update(topicId,headline,content,time,activeValue,actionAddress,actionPosition));
    }

    /**
     * @Description: 
     * @Author: SimpChaos 
     * @Date: 2022/7/21 8:13
     * @Param: [httpServletRequest, httpServletResponse, file]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/TopicImg")
    public APIResponse<?> updateTopicImg(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, MultipartFile file) throws IOException {
        String token = httpServletRequest.getHeader("Token");
        String uid = auth.getUidFromToken(token);
        String topicId = httpServletRequest.getParameter("aid");
        Integer number = Integer.parseInt(httpServletRequest.getParameter("index"));
        String result = topicService.updateImg(file,number,uid,topicId);
        return APIResponse.success("success",result);
    }


    /**
     * @Description: 撤销发布的活动
     * @Author: SimpChaos
     * @Date: 2022/7/16 1:35
     * @Param: [httpServletRequest, httpServletResponse]
     * @Return: com.example.a02back.common.APIResponse<?>
     */
    @CrossOrigin
    @RequestMapping("/deleteTop")
    public APIResponse<?> deleteTopic(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String topicId = httpServletRequest.getParameter("aid");
        String result = topicService.delete(topicId);
        return APIResponse.success("success",result);
    }








}
